/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.sapra.core.DBManager;
import org.sapra.dao.Departamento;
import org.sapra.dao.Empresa;
import org.sapra.logon.dLogin;

/**
 *
 * @author ppratsc
 */
public class dDepartamento {

   public static List<Departamento> getDepartamentos(Empresa emp) {
        List<Departamento> resp=null;
        String sql = "SELECT * FROM departamento WHERE fk_empresaId = " + emp.getEmpresaId();
        sql += " ORDER BY nombre";

        try {
            // Busca las Universidades
            resp = DBManager.getQuery(Departamento.class,sql);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resp;
    }

    static int NuevoDepto(String string){
        int resp=0;

        String sql= "INSERT INTO departamento(nombre, fk_empresaId) VALUES (";
        sql+="'"+string.toUpperCase().trim()+"'";
        sql+=","+dLogin.getEmpresa().getEmpresaId()+")";

         try {
            resp = DBManager.Update(sql);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resp;
    }

    static int borrarDepto(Departamento depto){
        int resp=0;
        String sql = "DELETE FROM departamento WHERE departamentoId = " + depto.getDepartamentoId();
        try {
            resp = DBManager.Update(sql);
            System.out.println(sql);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;


    }

        public static int editarDepartamento(Departamento depto,String nombre){
        int resp=0;
        String sql = "UPDATE departamento SET ";
        sql += "nombre = '" + nombre.toUpperCase().trim() + "'";
        sql += " WHERE departamentoId = " + depto.getDepartamentoId();
        try {
            resp = DBManager.Update(sql);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resp;
    }

  public static Departamento getDepartamento(int opc){
        Departamento resp = null;

        String sql = "SELECT ";
        sql += "* ";
        sql += " FROM ";
        sql += "departamento";
        sql += " WHERE ";
        sql += " departamentoId = " + opc;

        List<Departamento> deps;
        try {
            DBManager db=new DBManager();
            // Busca la universidad en la base de datos
            deps = db.getQuery(Departamento.class,sql);
            db=null;
            resp=new Departamento();
            resp=deps.get(0);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
            return resp;
        }

        return resp;
    }

    public static Departamento getDepartamento2(int opc){
        Departamento resp = null;

        String sql = "SELECT ";
        sql += "* ";
        sql += " FROM ";
        sql += "departamento";
        sql += " WHERE ";
        sql += " fk_empresaId = " + opc;

        List<Departamento> deps;
        try {
            DBManager db=new DBManager();
            // Busca la universidad en la base de datos
            deps = db.getQuery(Departamento.class,sql);
            db=null;
            resp=new Departamento();
            resp=deps.get(0);
        } catch (Exception ex) {
            Logger.getLogger(dDepartamento.class.getName()).log(Level.SEVERE, null, ex);
            return resp;
        }

        return resp;
    }

  
}
